#ifndef COMMON_H
#define COMMON_H

#include <stdint.h>
// 跨平台网络字节序头文件
#if defined(_WIN32)
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <arpa/inet.h>
#endif
#define START_KEY  "!@#$%^&"
#define END_KEY    "!@#$%^&"  // 对齐定义更美观

#pragma pack(push, 4)  // 注意括号要匹配

typedef struct _HOST_INFO {
    char address[16];   // 修正拼写错误：addree -> address
    uint16_t port;
    uint16_t protocol;
} HOST_INFO;

typedef struct _CONFIG {
    char start[8];      // 对齐更整齐
    HOST_INFO host_info;
    char end[8];
} CONFIG;
// 转换函数声明
void host_to_network_host_info(HOST_INFO* host);
void network_to_host_host_info(HOST_INFO* host);
void host_to_network_config(CONFIG* config);
void network_to_host_config(CONFIG* config);

// HOST_INFO的字节序转换
void host_to_network_host_info(HOST_INFO* host) {
    host->port = htons(host->port);          // 16位转换
    host->protocol = htons(host->protocol);  // 16位转换
    // address是字符数组，无需转换
}

void network_to_host_host_info(HOST_INFO* host) {
    host->port = ntohs(host->port);          // 16位转回主机序
    host->protocol = ntohs(host->protocol);  // 16位转回主机序
}

// CONFIG的字节序转换
void host_to_network_config(CONFIG* config) {
    // 先转换内嵌的HOST_INFO
    host_to_network_host_info(&config->host_info);
    // start/end是字符数组，无需转换
}

void network_to_host_config(CONFIG* config) {
    // 先转换内嵌的HOST_INFO
    network_to_host_host_info(&config->host_info);
    // start/end是字符数组，无需转换
}
#pragma pack(pop)       // 恢复对齐设置

#endif // COMMON_H
